<?php

App::uses('AppController', 'Controller');
App::import('Model', 'Image');
App::import('Controller', 'Images');

/**
 * Items Controller
 *
 * @property Item $Item
 */
class ItemsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        
        $this->Item->recursive = 0;
        $Image=new Image();
        $images=$Image->find('list', array('fields' => array('Image.id', 'Image.file_name')));
        $items=$this->paginate();
        
        foreach($items as $indice => $it)
        {
            
            $aux=array('file_name' => $images[$it['Item']['id_image']]);
            array_push($items[$indice]['Item'],$aux);
        }
        $this->set('items', $items);
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        
        $this->Item->id = $id;
        if (!$this->Item->exists()) {
            throw new NotFoundException(__('Invalid item'));
        }
        
        $this->Items->recursive = 0;
        $Image=new Image();
        $items=$this->Item->findById($id);
        $images=$Image->findById($items['Item']['id_image'], array('fields' => 'Image.file_name'));
        $auxImage=array('file_name' => $images['Image']['file_name']);
        array_push($items['Item'],$auxImage);
        $this->set('items', $items);
        //return $items;
    }

    /**
     * add method
     *
     * @return void
     */
    
    public function add() {
        if ($this->request->is('post')) {
            
            //Cria entidade Item
            $this->Item->create();
            
            //Cria uma imagem
            $this->Images = new ImagesController;
            $this->Images->constructClasses();
            
             $image['Image']['Image']=$this->request->data['Item']['Image'];
             
             $error=false;
             
             if(!empty($this->request->data['Item']['Image']['tmp_name']) && !empty($this->request->data['Item']['item_name'])){
                    if(!empty($this->request->data['Item']['description']) && !empty($this->request->data['Item']['type'])){
                        $id_image=$this->Images->add($image);
                    }else $error=true;
            }else $error=true;
             
            
            if($error){
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                if(!empty($this->request->data['Item']['Image']['tmp_name']))
                    $this->request->data['Item']['Image']="error";
                $this->Item->save($this->request->data);
                return;
            }
            
            $this->request->data['Item']['id_image']=$id_image['Image']['id'];
            $this->request->data['Item']['Image']="default";
            
             if ($this->Item->save($this->request->data)) {
                $this->Session->setFlash('Item cadastrado com sucesso','default',array('class' => 'sucessMsg'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
            }
        }
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Item->id = $id;
        if (!$this->Item->exists()) {
            throw new NotFoundException(__('Invalid item'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Item->save($this->request->data)) {
                $this->Session->setFlash(__('Item cadastro sucesso','default',array('class' => 'sucessMsg')));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
            }
        } else {
            $this->request->data = $this->Item->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Item->id = $id;
        if (!$this->Item->exists()) {
            throw new NotFoundException(__('Invalid item'));
        }
        if ($this->Item->delete()) {
            $this->Session->setFlash('Item excluído com sucesso','default',array('class' => 'sucessMsg'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Erro na exclusão do item','default',array('class' => 'errorMsg'));
        $this->redirect(array('action' => 'index'));
    }

}
